﻿using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.MobileInterface.Common;
using SXT.Party.JoinParty.BLL;
using SXT.Party.Join.Model;
using SXT.Party.JoinParty.Model;

namespace ZHDJ.MobileInterface
{
    //入党申请
    public partial class ApplyJoinParty : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ApplyJoinPartyOutParam result = new ApplyJoinPartyOutParam();
            ApplyJoinPartyInParam param = new ApplyJoinPartyInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as ApplyJoinPartyInParam;

                string CheckResult = CheckValueIsNull(param);
                if (!string.IsNullOrEmpty(CheckResult))
                {
                    result.code = "0";
                    result.desc = CheckResult;
                    isFlag = false;
                }
                if (isFlag)
                {
                    if (!SaveApplyJoinParty(param, out CheckResult))
                    {
                        result.code = "0";
                        result.desc = CheckResult;
                    }
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "转移过程出现异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-ApplyJoinParty", SerializeHelper.JsonSerializer<ApplyJoinPartyInParam>(param), logMsg);
            return result as OutParam;
        }

        /// <summary>
        /// 保存入党申请
        /// </summary>
        /// <param name="param">数据源</param>
        /// <param name="CheckResult">保存失败原因</param>
        /// <returns></returns>
        private bool SaveApplyJoinParty(ApplyJoinPartyInParam param, out string CheckResult)
        {

            if (!ApplyJoinPartyHelper.IsNewApplyByLoginID(param.LoginID))
            {
                CheckResult = string.Format("用户{0}已经提交申请！", param.UserName);
                return false;
            }
            param.AppllicationID = StringHelper.NewID;
            if (ApplyJoinPartyHelper.InsertApply(param)) //插入入党申请数据
            {
                if (!ApplyJoinPartyHelper.IsExistOrgMember(param.LoginID))
                {
                    ApplyJoinPartyHelper.UpdateOrgMember(param);
                }
                else
                {
                    ApplyJoinPartyHelper.InsertOrgMember(param);
                }

                ApplyJoinPartyHelper.InsertLinkExample(param.AppllicationID, param.LoginID, param.UserName);
                CheckResult = string.Empty;
                return true;
            }
            else
            {
                CheckResult = "插入数据失败！";
                return false;
            }
        }

        /// <summary>
        /// 检查数据为空
        /// </summary>
        /// <param name="param">请求参数数据</param>
        /// <returns></returns>
        private string CheckValueIsNull(ApplyJoinPartyInParam param)
        {
            if (string.IsNullOrEmpty(param.LoginID))
                return "用户登录ID不能为空！";
            DateTime dtBirthday = DateTime.Now;
            try
            {
                dtBirthday = Convert.ToDateTime(param.Birthday);
            }
            catch
            {
                return "有效时间不是有效日期格式数据";
            }
            if (string.IsNullOrEmpty(param.Nation))
                return "民族不能为空！";
            if (string.IsNullOrEmpty(param.NativePlace))
                return "籍贯不能为空！";
            if (string.IsNullOrEmpty(param.Educational))
                return "文化程度不能为空！";
            if (string.IsNullOrEmpty(param.Address))
                return "居住地址不能为空！";
            if (string.IsNullOrEmpty(param.StudyyAndWorkCompany))
                return "学习工作单位不能为空！";
            if (string.IsNullOrEmpty(param.WorkAddress))
                return "工作所在地不能为空！";
            if (string.IsNullOrEmpty(param.BranchID))
                return "所属党组织不能为空！";
            if (string.IsNullOrEmpty(param.Content))
                return "内容不能为空！";
            return "";
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ApplyJoinPartyInParam, ApplyJoinPartyOutParam>();
            else
                SafeProcessRequest<ApplyJoinPartyInParam, ApplyJoinPartyOutParam>();
        }
    }

    #region 请求参数
    public class ApplyJoinPartyInParam
    {
        #region 属性
        /// <summary>
        /// AppllicationID
        /// </summary>
        public string AppllicationID { get; set; }

        /// <summary>
        /// 申请人ID
        /// </summary>
        public string LoginID { get; set; }

        /// <summary>
        /// UserName
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public string Gender { get; set; }

        /// <summary>
        /// 身份证号码
        /// </summary>
        public string IdentityCard { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public string Birthday { get; set; }

        /// <summary>
        /// 民族
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 籍贯
        /// </summary>
        public string NativePlace { get; set; }

        /// <summary>
        /// 文化程度
        /// </summary>
        public string Educational { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string MobileNo { get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// QQ号码
        /// </summary>
        public string QQNumber { get; set; }

        /// <summary>
        /// 飞信号
        /// </summary>
        public string feitionNo { get; set; }

        /// <summary>
        /// 居住地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 学习工作单位
        /// </summary>
        public string StudyyAndWorkCompany { get; set; }

        /// <summary>
        /// 工作所在地
        /// </summary>
        public string WorkAddress { get; set; }

        /// <summary>
        /// CreateTime 递交申请时间
        /// </summary>
        public string CreateTime { get; set; }

        /// <summary>
        /// 所属党支部
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 状态(0:草稿，1：申请中，2：申请成功，3：申请取消)
        /// </summary>
        public int Status { get; set; }
        #endregion
    }
    #endregion

    #region 结果参数
    public class ApplyJoinPartyOutParam
    {
        public ApplyJoinPartyOutParam()
        {
            code = "1";
            desc = "操作成功";
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }
    }
    #endregion


}